require 'gadgets/radio_group'
require 'event_editor/gadgets/switch_button'
module EventEditor
  module Commands
    class Variable
      def self.show(base, model, edit_command = [], options = {}, &func)
       @dialog = Gtk::Dialog.new("Variable",
               base,
               Gtk::Dialog::MODAL,
               ["OK", Gtk::Dialog::RESPONSE_OK],
               ["Cancel", Gtk::Dialog::RESPONSE_CANCEL]
               )

        @model = model

        @operands = {}
        @dialog.vbox.add(self.variable_selecter(base))
        @dialog.vbox.add(self.operator_chooser)
        @dialog.vbox.add(self.operand_chooser(base))
        @dialog.vbox.show_all
        
        self.load(edit_command)
        
        response = @dialog.run
        if response == Gtk::Dialog::RESPONSE_OK
          command = self.command
          func.call(command) 
        end
        @dialog.destroy
      end
      
      def self.variable_selecter(base)
        frame = Gtk::Frame.new('Variable')
        box = Gtk::HBox.new
        @variable_input = EventEditor::Gadgets::SwitchButton.new(base, @model.variables) do |arr|
          @model.variables = arr
        end
        box.pack_start(@variable_input, false, false)
        frame << box
        return frame
      end
      
      def self.operator_chooser
        frame = Gtk::Frame.new('Operators')
        @operator_group = ::Gadgets::RadioGroup.new(:horizontal)
        @operator_group.add_element("=")
        @operator_group.add_element("+")
        @operator_group.add_element("-")
        @operator_group.add_element("/")
        @operator_group.add_element("*")
        @operator_group.add_element("%")
        frame << @operator_group
        return frame
      end
      
      def self.operand_chooser(base)
        frame = Gtk::Frame.new('Operands')
        @operand_group = ::Gadgets::RadioGroup.new(:vertical)
        
        @operands["var"] = EventEditor::Gadgets::SwitchButton.new(base, @model.variables) do |arr|
          @model.variables = arr
        end
        @operand_group.add_element("var", "", @operands["var"])
        
        @operands["val"] = Gtk::SpinButton.new(0, 99999, 1)
        @operand_group.add_element("val", "", @operands["val"])
        
        frame << @operand_group
        return frame
      end
      
      def self.load(edit_command)
        unless edit_command.empty?
          @variable_input.value = edit_command[1]
          @operator_group.selected_key = edit_command[2]
          @operand_group.selected_key = edit_command[3]
          @operands[edit_command[3]].value = edit_command[4]
        end
      end
      
      def self.command
        return ['variable', @variable_input.value, @operator_group.selected_key, @operand_group.selected_key, @operands[@operand_group.selected_key].value.to_i]
      end
      
      def self.command_to_s(model, args, margin = "")
        s = ""
        s += '<b>Variable:</b> '
        s += "[" + args[1].to_s + ": " + model.variables[args[1]] + "]" + " #{args[2]}= "

        case args[3]
        when "var"
          s += "[" + args[4].to_s + ": " + model.variables[args[4].to_i] + "]"
        else
          s += args[4].to_s
        end
        return s
      end
    end
  end
end